# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=vim
_topver=8.1
_patchlevel=0022
_versiondir="${pkgname}${_topver//./}"
pkgver=${_topver}.${_patchlevel}
pkgrel=1
arch=('i686' 'x86_64')
license=('custom:vim')
url="http://www.vim.org"
depends=('ncurses')
groups=('editors')
makedepends=('gawk' 'perl' 'python2' 'python3' 'ruby' 'libiconv' 'ncurses-devel' 'libcrypt-devel')
source=(${pkgname}-${pkgver}.tar.gz::https://github.com/vim/vim/archive/v${pkgver}.tar.gz
        'dot.vimrc'
        '7.3-cygwin-mouse.patch'
        '7.3-virc.patch'
        '7.3-cygwin-python-dyn.patch'
        'pretend-cygwin-msys.patch'
        'accept-crlf.patch'
        'vim-completion')
sha256sums=('28b5d850d4df455f41c457399a578b1da6cca2bcc79115498356aa0afd8ca460'
            'edd18e0756406ee7b659e4552e444c50c3a0c1e9fb4fce2ddd770c933ea6c7f5'
            'bca6e030d50c0d2468ab5c78aa0b359eb18a88a197de8406c593458ffedde922'
            '44d7738a8f801195898eeef766ff77506c717dd5d19145ade3c1c2349d4bc4fd'
            '0698234a2436f2d37f43775c9db5a2fc67d182f8baa46a8a0e2b7dd37da6fcbd'
            '5fcec194d2fcb2a624358d8ce074e8d97b873b5c2fff2118491b57a4880737d9'
            'bfa0b1f29e83eadd161c570f28a1247e3dca6aa3d89d23abcb7e57dab7649a6d'
            'bdca6069ef0fa995718f4b59fea85e58629259bb5a385d53e52d162d1463d4ff')

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}

  iconv -f ISO-8859-1 -t UTF-8 runtime/doc/eval.txt \
    > runtime/doc/eval.tmp && mv -f runtime/doc/eval.{tmp,txt}

  patch -p2 -i ${srcdir}/7.3-cygwin-mouse.patch
  patch -p2 -i ${srcdir}/7.3-virc.patch
  patch -p2 -i ${srcdir}/7.3-cygwin-python-dyn.patch
  patch -p1 -i ${srcdir}/pretend-cygwin-msys.patch
  patch -p1 -i ${srcdir}/accept-crlf.patch

  # define the place for the global (g)vimrc file (set to /etc/vimrc)
  #sed -i 's|^.*\(#define SYS_.*VIMRC_FILE.*"\) .*$|\1|' \
  #  vim-build/src/feature.h
  #sed -i 's|^.*\(#define VIMRC_FILE.*"\) .*$|\1|' \
  #  vim-build/src/feature.h

  cd src
  autoconf
}

build() {
  cd ${srcdir}/${pkgname}-${pkgver}

  # ./configure \
    # --prefix=/usr \
    # --build=${CHOST} \
    # --with-features=small \
    # --with-tlib=ncursesw \
    # --enable-multibyte \
    # --disable-luainterp \
    # --disable-perlinterp \
    # --disable-pythoninterp \
    # --disable-python3interp \
    # --disable-rubyinterp \
    # --disable-tclinterp \
    # --disable-gpm --disable-sysmouse \
    # --disable-gui --without-x \
    # --with-compiledby="<alexpux@gmail.com>"
  # make auto/osdef.h
  # make VIMRCLOC=/etc VIMRUNTIMEDIR=/usr/share/vim/vim${slot/\.} || die "make 2 fail"
  # mv vim.exe vi.exe
  # make distclean

  ./configure \
    --prefix=/usr \
    --build=${CHOST} \
    --with-features=huge \
    --with-tlib=ncursesw \
    --enable-cscope \
    --enable-multibyte \
    --enable-luainterp=dynamic \
    --enable-perlinterp=dynamic \
    --enable-pythoninterp=dynamic \
    --enable-python3interp=dynamic \
    --enable-rubyinterp=dynamic \
    --disable-tclinterp \
    --disable-gpm --disable-sysmouse \
    --disable-gui --without-x \
    CPPFLAGS="${CPPFLAGS} -I/usr/include/ncursesw" \
    --with-compiledby="<alexpux@gmail.com>"

  #make auto/osdef.h
  make -j1 VIMRCLOC=/etc VIMRUNTIMEDIR=/usr/share/vim/vim${_topver/\.}
}

check() {
  # disable tests because they seem to freeze

  cd "${srcdir}"/${pkgname}-${pkgver}

  #make test
}

package() {
  cd "${srcdir}"/${pkgname}-${pkgver}
  make -j1 VIMRCLOC=/etc DESTDIR="${pkgdir}" install

  make install -j1 \
    VIMRCLOC=/etc VIMRUNTIMEDIR=/usr/share/vim/vim${_topver/\.} \
    STRIP=: INSTALL_DATA="cp -L" INSTALL_DATA_R="cp -Lr" \
    DESTDIR="${pkgdir}"

  #for x in ex rvi vi.exe
  #do
  #  cp -f vi.exe ${pkgdir}/usr/bin/${x}
  #done

  for y in vimdiff view rview rvim
  do
    cp -f ${pkgdir}/usr/bin/vim.exe ${pkgdir}/usr/bin/${y}
  done

  mkdir -p ${pkgdir}/usr/share/bash-completion/completions
  cp -f ${srcdir}/vim-completion ${pkgdir}/usr/share/bash-completion/completions/vim

  # delete some manpages
  find "${pkgdir}"/usr/share/man -type d -name 'man1' 2>/dev/null | \
    while read _mandir; do
    cd ${_mandir}
    rm -f ex.1 # provided by (n)vi
    rm -f evim.1    # this does not make sense if we have no GUI
  done

  # fix FS#17216
  sed -i 's|messages,/var|messages,/var/log/messages.log,/var|' \
    "${pkgdir}"/usr/share/vim/${_versiondir}/filetype.vim

  # patch filetype.vim for better handling of pacman related files
  sed -i "s/rpmsave/pacsave/;s/rpmnew/pacnew/;s/,\*\.ebuild/\0,PKGBUILD*,*.install/" \
    "${pkgdir}"/usr/share/vim/${_versiondir}/filetype.vim
  sed -i "/find the end/,+3{s/changelog_date_entry_search/changelog_date_end_entry_search/}" \
    "${pkgdir}"/usr/share/vim/${_versiondir}/ftplugin/changelog.vim

  # rc files
  install -Dm644 "${srcdir}"/dot.vimrc "${pkgdir}"/etc/skel/.vimrc

  # rgb.txt file
  install -Dm644 "${srcdir}"/vim-${pkgver}/runtime/rgb.txt \
    "${pkgdir}"/usr/share/vim/${_versiondir}/rgb.txt

  # license
  install -Dm644 "${srcdir}"/vim-${pkgver}/runtime/doc/uganda.txt \
    "${pkgdir}"/usr/share/licenses/${pkgname}/license.txt
}
